package com.shenhaofeng.app.component.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

import com.shenhaofeng.app.camera.Option;
import com.shenhaofeng.app.component.BaseActivity;
import com.shenhaofeng.app.component.BaseFragment;


/**
 * 一个选取图片数据源的弹窗
 * Created by Administrator on 2016/9/8.
 */
public class ImageSourceItemDialog extends DialogFragment {


    private static final String ARGS_TITLE = "title";

    private static final String ARGS_THEME = "theme";

    private static final String ARGS_URI = "uri";

    private static final String ARGS_REQUEST_CAMERA = "request_camera_code";

    private static final String ARGS_REQUEST_ALBUM = "request_album_code";

    private CharSequence mTitle;

    private Uri mUri;

    private int mThemeRes;

    private int mCameraRequestCode;

    private int mAlbumRequestCode;

    private BaseActivity mBaseActivity;

    private BaseFragment mBaseFragment;


    /**
     * 创建实例
     *
     * @param title
     * @param themeRes
     * @param cameraRequestCode
     * @param albumRequestCode
     * @return
     */
    public static ImageSourceItemDialog createInstance(CharSequence title, Uri uri, int cameraRequestCode, int albumRequestCode, @StyleRes int themeRes) {
        ImageSourceItemDialog imageSourceItemDialog = new ImageSourceItemDialog();
        Bundle bundle = new Bundle();
        bundle.putCharSequence(ARGS_TITLE, title);
        bundle.putParcelable(ARGS_URI, uri);
        bundle.putInt(ARGS_THEME, themeRes);
        bundle.putInt(ARGS_REQUEST_CAMERA, cameraRequestCode);
        bundle.putInt(ARGS_REQUEST_ALBUM, albumRequestCode);
        imageSourceItemDialog.setArguments(bundle);
        return imageSourceItemDialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitle = getArguments() == null ? "选择图片" : getArguments().getCharSequence(ARGS_TITLE, "选择图片");
        mUri = getArguments() == null ? null : (Uri) getArguments().getParcelable(ARGS_URI);
        mThemeRes = getArguments() == null ? getTheme() : getArguments().getInt(ARGS_THEME, getTheme());
        mCameraRequestCode = getArguments() == null ? getTheme() : getArguments().getInt(ARGS_REQUEST_CAMERA, 1);
        mAlbumRequestCode = getArguments() == null ? getTheme() : getArguments().getInt(ARGS_REQUEST_ALBUM, 2);
        if (getParentFragment() != null && getParentFragment() instanceof BaseFragment) {
            mBaseFragment = (BaseFragment) getParentFragment();
        } else if (getActivity() instanceof BaseActivity) {
            mBaseActivity = (BaseActivity) getActivity();
        } else {
            throw new RuntimeException("ImageSourceItemDialog的父Fragment和Activity必须有一个为BaseFragment或者BaseActivity");
        }

    }


    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (mBaseFragment == null && mBaseActivity == null) {
            return new Dialog(getContext(), getTheme());
        }
        return new AlertDialog.Builder(getContext(), mThemeRes)
                .setTitle(mTitle)
                .setItems(new CharSequence[]{"拍照", "从相册选择"}, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0: {
                                if (mBaseFragment == null && mBaseActivity == null) {
                                    return;
                                } else if (mBaseFragment != null) {
                                    //在BaseFragment中调用
                                    mBaseFragment.openCamera(mCameraRequestCode
                                            , new Option.Builder()
                                                    .outputUri(mUri)
                                                    .build());
                                } else if (mBaseActivity != null) {
                                    //在BaseActivity中调用
                                    mBaseActivity.openCamera(mCameraRequestCode
                                            , new Option.Builder()
                                                    .outputUri(mUri)
                                                    .build());
                                }
                                break;
                            }
                            case 1: {
                                if (mBaseFragment == null && mBaseActivity == null) {
                                    return;
                                } else if (mBaseFragment != null) {
                                    //在BaseFragment中调用
                                    mBaseFragment.openAlbum(mAlbumRequestCode);
                                } else if (mBaseActivity != null) {
                                    //在BaseActivity中调用
                                    mBaseActivity.openAlbum(mAlbumRequestCode);
                                }
                                break;
                            }
                        }
                    }
                })
                .create();
    }


}
